

const { getClient } = require('./initDbClient')

const tableName = "plugin_info"
const model = {
    pluginId: "string",
    groupId: "string",
    artifactId: "string",
    version: "string",
    pluginInfo: "json"
}


function initTable(callback, force = false) {
    if (force) {
        sql = `drop table  if exists ${tableName} ; create table ${tableName} 
            ( 
                pluginId varchar(128), 
                groupId varchar(256), 
                artifactId varchar(256),
                version varchar(128),
                pluginInfo text
            )`
    } else {
        sql = `create table  if not exists ${tableName} 
            ( 
                pluginId varchar(128), 
                groupId varchar(256), 
                artifactId varchar(256),
                version varchar(128),
                pluginInfo text
            )`
    }
    getClient().createTable(sql, callback)
}

function do2Models(objs) {
    const data = []
    for (let obj of objs) {
        data.push(do2Model(obj))
    }
    return data
}

function model2Dos(objs) {
    const data = []
    for (let obj of objs) {
        data.push(model2Do(obj))
    }
    return data
}


function do2Model(obj) {
    let model = {}

    Object.keys(obj).forEach(function (key) {
        if (key == "pluginInfo") {
            model[key] = JSON.parse(obj[key])
        } else {
            model[key] = obj[key]
        }
    });
    return model

}

function model2Do(obj) {
    let DO = {}
    Object.keys(obj).forEach(function (key) {
        if (key == "pluginInfo") {
            DO[key] = JSON.stringify(obj[key])
        } else {
            DO[key] = obj[key]
        }
    });

    return DO

}

exports.insert = function (model) {
    const obj = model2Do(model)
    const dataItem = []
    const keys = []
    const value = []
    Object.keys(obj).forEach(function (key) {
        dataItem.push(obj[key])
        keys.push(key)
        value.push("?")
    });

    theKeys = keys.join(",")
    theValue = value.join(",")
    const insertTileSql = `insert into  ${tableName} (${theKeys}) values(${theValue})`;
    initTable(() => {
        getClient().insertData(insertTileSql, [dataItem]);
    })
}


exports.update = function (model) {
    const obj = model2Do(model)

    exports.getPluginInfoByCondition(obj.pluginId, null, null, (pluginInfos) => {
        if (pluginInfos.length == 0) {
            exports.insert(model)
        } else {
            let thePluginInfo = pluginInfos[0]
            const pluginId = thePluginInfo.pluginId
            const pluginInfo = obj.pluginInfo
            updateSql = `update ${tableName} set pluginInfo = '${pluginInfo}' where pluginId = '${pluginId}'`;
            initTable(() => {
                getClient().executeSql(updateSql)
            })

        }
    })
}

exports.getPluginInfoByCondition = function (pluginId, groupId, artifactId, callback) {
    let sql = `select * from ${tableName} where 1=1`
    if (pluginId != null) {
        sql = sql + ` and pluginId= '${pluginId}'`
    }
    if (groupId != null) {
        sql = sql + ` and groupId= '${groupId}'`
    }
    if (artifactId != null) {
        sql = sql + ` and artifactId= '${artifactId}'`
    }
    initTable(() => {
        console.info("sql", sql)
        getClient().queryData(sql, (data) => {
            callback(do2Models(data))
        })
    })

}



exports.composeModel = function (pluginId, groupId, artifactId, version, pluginInfo) {
    return {
        pluginId: pluginId,
        groupId: groupId,
        version: version,
        artifactId: artifactId,
        pluginInfo: pluginInfo
    }
}